﻿using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Hxsoft.AppV1;

namespace Hxsoft.AppV1._Controls
{
	public partial class CheckCodeHelper : System.Web.UI.UserControl
	{
		/// <summary>
		/// 验证码的宽度。
		/// </summary>
		public Unit Width
		{
			get { return CheckCodeImage.Width; }
			set { CheckCodeImage.Width = value; }
		}

		/// <summary>
		/// 验证码的高度。
		/// </summary>
		public Unit Height
		{
			get { return CheckCodeImage.Height; }
			set { CheckCodeImage.Height = value; }
		}

		/// <summary>
		/// 验证码字体大小。
		/// </summary>
		public int? FontSize
		{
			get { return (int?)ViewState["FontSize"]; }
			set { ViewState["FontSize"] = value; }
		}

		/// <summary>
		/// 是否显示重新加载图片按钮。
		/// </summary>
		public bool ShowReloadButton
		{
			get { return ReloadButtonContainer.Visible; }
			set { ReloadButtonContainer.Visible = value; }
		}

		/// <summary>
		/// 重新加载图片按钮的文本。
		/// </summary>
		public string ReloadButtonText
		{
			get
			{
				object o = ViewState["ReloadButtonText"];
				return o == null ? Res.CheckCodeHelper_ReloadButtonText : (string)o;
			}
			set { ViewState["ReloadButtonText"] = value; }
		}

		private string _imageUrlSeparator = "?";
		protected string ImageUrlSeparator
		{
			get { return _imageUrlSeparator; }
			set { _imageUrlSeparator = value; }
		}

		/// <summary>
		/// 验证码图片的URL地址。
		/// </summary>
		protected string ImageUrl
		{
			get
			{
				ImageUrlSeparator = "?";
				string imgUrl = ApplicationConfiguration.CheckCodeImageUrl;
				if (FontSize.HasValue)
				{
					imgUrl += ImageUrlSeparator + "FontSize=" + FontSize.Value;
					ImageUrlSeparator = "&";
				}
				return this.ResolveUrl(imgUrl);
			}
		}

		/// <summary>
		/// 重新加载图片的客户端脚本函数名。
		/// </summary>
		protected string ReloadClientScriptFunctionName
		{
			get
			{
				return this.ClientID + "_ReloadCheckCode";
			}
		}

		/// <summary>
		/// 重新加载图片的客户端脚本。
		/// </summary>
		public string ReloadClientScript
		{
			get
			{
				return ReloadClientScriptFunctionName + "();";
			}
		}

		protected override void OnPreRender(EventArgs e)
		{
			CheckCodeImage.ImageUrl = ImageUrl;
			CheckCodeImage.AlternateText = Res.CheckCodeHelper_CheckCodeImageAltText;
			
			base.OnPreRender(e);
		}
	}
}